package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.*;
import com.pig4cloud.pig.admin.api.req.InfusionCoreChangeReq;
import com.pig4cloud.pig.admin.api.req.InfusionCoreListReq;
import com.pig4cloud.pig.admin.api.resp.*;
import com.pig4cloud.pig.admin.service.InfusionCoreService;
import com.pig4cloud.pig.admin.service.InfusionSeatService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/infusion")
@Tag(description = "infusion", name = "输液管理模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class InfusionController {
	//
	private final InfusionSeatService infusionSeatService ;
	//
	private final InfusionCoreService infusionCoreService ;

	/**
	 * 通过ID查询
	 * @param id ID
	 * @return InfusionSeat
	 */
	@GetMapping("/seat/{id}")
	public R<InfusionSeat> getSeatById(@PathVariable Long id) {
		return R.ok(infusionSeatService.getById(id));
	}

	/** 查询全部座位 */
	@GetMapping("/seat/list")
	public R<List<InfusionSeat>> seatList() {
		return R.ok(infusionSeatService.list());
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param infusionSeat +信息表
	 * @return 分页列表
	 */
	@Operation(description = "分页查询", summary = "分页查询")
	@GetMapping(value = "/seat/page")
	@PreAuthorize("@pms.hasPermission('infusion_seat_view')")
	public R<Page<InfusionSeat>> getSeatPage(@ParameterObject Page<InfusionSeat> page, @ParameterObject InfusionSeat infusionSeat) {
		page.setOrders(null);
		Page<InfusionSeat> voPage = infusionSeatService.page(page, Wrappers.<InfusionSeat>lambdaQuery()
				.like(StrUtil.isNotBlank(infusionSeat.getSeatName()), InfusionSeat::getSeatName, infusionSeat.getSeatName())
				.orderByAsc(InfusionSeat::getCreateTime));
		return R.ok(voPage);
	}

	/**
	 * 添加 座位
	 * @param infusionSeat 实体
	 * @return success/false
	 */
	@SysLog(value = "添加座位")
	@PostMapping(value = "/seat")
	@PreAuthorize("@pms.hasPermission('infusion_seat_add')")
	public R<Boolean> saveSeat(@Valid @RequestBody InfusionSeat infusionSeat) {
		return R.ok(infusionSeatService.save(infusionSeat));
	}

	/**
	 * 删除
	 * @param id ID
	 * @return success/false
	 */
	@SysLog(value = "删除座位")
	@DeleteMapping(value = "/seat/{id}")
	@PreAuthorize("@pms.hasPermission('infusion_seat_del')")
	public R<Boolean> removeSeatById(@PathVariable Long id) {
		return R.ok(infusionSeatService.removeById(id));
	}

	/**
	 * 编辑
	 * @param infusionSeat 实体
	 * @return success/false
	 */
	@SysLog(value = "编辑座位")
	@PutMapping(value = "/seat")
	@PreAuthorize("@pms.hasPermission('infusion_seat_edit')")
	public R<Boolean> updateSeat(@Valid @RequestBody InfusionSeat infusionSeat) {
		return R.ok(infusionSeatService.updateById(infusionSeat));
	}

	@SysLog(value = "导出座位")
	@ResponseExcel
	@GetMapping("/seat/export")
	public List<InfusionSeat> seatExport(InfusionSeat infusionSeat) {
		return infusionSeatService.list(Wrappers.query(infusionSeat));
	}

	@SysLog(value = "添加输液信息")
	@PutMapping(value = "/add/seat/infusion")
	public R<InfusionCore> addSeatInfusion(@Valid @RequestBody InfusionCore infusionCore) {
		InfusionCore bool = infusionCoreService.add(infusionCore);
		return R.ok(bool);
	}

	@SysLog(value = "输液人员匹配座位")
	@PostMapping(value = "/change/seat/infusion")
	public R<Boolean> changeSeatInfusion(@Valid @RequestBody InfusionCoreChangeReq infusionCoreChangeReq) {
		Boolean bool = infusionCoreService.change(infusionCoreChangeReq);
		return R.ok(bool);
	}

	@SysLog(value = "输液列表模式")
	@GetMapping(value = "/list")
	public R<Page<InfusionCoreListResp>> getInfusionList(@ParameterObject Page<InfusionCore> page,
														 @ParameterObject InfusionCoreListReq listReq) {
		String infusionDate = listReq.getInfusionDate();
		Page<InfusionCoreListResp> pageVo = infusionCoreService.pageVo(page, infusionDate);
		return R.ok(pageVo);
	}

	@SysLog(value = "输液统计")
	@GetMapping(value = "/statistics")
	public R<InfusionCoreStatisticsVO> statistics(@RequestParam String infusionDate) {
		InfusionCoreStatisticsVO statisticsVO = infusionCoreService.statistics(infusionDate);
		return R.ok(statisticsVO);
	}

	@SysLog(value = "输液座位模式")
	@GetMapping(value = "/seat")
	public R<InfusionSeatRespVO> getInfusionSeat(@RequestParam String infusionDate) {
		List<ToBeInfusedList> toBeInfused = infusionCoreService.getToBeInfused(infusionDate);
		List<InfusionSeatList> seats = infusionCoreService.getSeats(infusionDate);
		InfusionSeatRespVO respVO = new InfusionSeatRespVO();
		respVO.setToBeInfused(toBeInfused);
		respVO.setSeats(seats);
		return R.ok(respVO);
	}

	@SysLog(value = "获取输液信息")
	@GetMapping(value = "/get/infusionCore")
	public R<InfusionCore> getInfusion(@RequestParam String coreId) {
		InfusionCore infusionCore = infusionCoreService.getById(coreId);
		return R.ok(infusionCore);
	}

	@SysLog(value = "取消预约")
	@DeleteMapping(value = "/core/cancel")
	public R<Boolean> coreCancel(@RequestParam String coreId) {
		Boolean bool = infusionCoreService.coreCancel(coreId);
		return R.ok(bool);
	}

	@SysLog(value = "获取同一时间段内的座位数量")
	@PostMapping(value = "/check/seat/num")
	public R<Integer> checkSeatNumber(@Valid @RequestBody InfusionCore infusionCore) {
		Integer seatNumber = infusionCoreService.checkSeatNumber(infusionCore);
		return R.ok(seatNumber);
	}

}
